package com.ymttest.common.order.userquery.client;


import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.util.StringUtils;

import com.ymt.core.tool.Logger;
import com.ymttest.common.order.constants.Constants;
import com.ymttest.common.order.userquery.config.BizConfig;
import com.ymttest.common.order.userquery.util.HttpClientUtil;

/**
 * Created by zhangzhiqiang on 2016/10/28.
 */
public class LogisticsClient {

    private BizConfig bizConfig = new BizConfig();

    /**
     * 验证该订单是否有一个包裹被国内签收
     * @param orderId
     * @param userId
     * @return
     */
    public boolean checkDemosticSigned( long orderId, long userId ) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("OrderId", orderId);
        map.put("BuyerId", userId);
        map.put("CallerAppId", Constants.APP_ID);

        String url = bizConfig.getLogisticsUrl() + Constants.GET_DEMOSTIC_SIGNED_SUFFIX;
        String respStr = "";
        try {
            respStr = HttpClientUtil.requestPostJson(url, map);
        } catch (Exception ex) {
        	Logger.fail(ex);
            return false;
        }

        if (!StringUtils.hasText(respStr)) {
        	Logger.fail(new Exception("checkDemosticSigned error,Resp is empty"));
            return false;
        }
        Logger.debug(String.format("checkDemosticSigned[req:{ %s }，Response:{ %s }", map, respStr));


        JSONObject jo = null;
        try {
            jo = JSONObject.fromObject(respStr);
            String body = jo.getString("data");
            if (StringUtils.hasText(body)) {
                jo = JSONObject.fromObject(body);
                return jo.getBoolean("domesticDelivered");
            } else {
                return false;
            }
        } catch (Exception e) {
        	Logger.fail(e);
            return false;
        }
    }

}
